package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Assessment;
import cn.itcast.service.*;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    MemberService service;
    @Reference
    OrderSettingService orderSettingService;
    @Reference
    AssessmentService assessmentService;

    @Reference
    UserService userService;

    @RequestMapping("/getMemberReport.do")
    public Result getMemberReport() throws Exception {

        //创建集合用于存放月份
        List<String> months = new ArrayList<>();
        //获取月数信息
        Calendar calendar = Calendar.getInstance();
        //2018-11
        calendar.add(calendar.MONTH, -13);//负数指 向前推出指定的月数  正数指 向后推出指定的月数
        for (int i = 0; i <= 12; i++) {
            calendar.add(calendar.MONTH, 1);
            months.add(DateUtils.parseDate2String(calendar.getTime(), "yyyy.MM"));
        }

        //获取月数对应的会员人数
        List<Integer> list = service.findMemberCountByMonths(months);

        //将月数与会员数载入到map集合中
        Map<String, Object> map = new HashMap<>();
        map.put("months", months);
        map.put("memberCount", list);


        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }


    @Reference
    SetmealService setmealService;

    @RequestMapping("/getSetmealReport.do")
    public Result getSetmealReport() throws Exception {

        Map<String, Object> map = setmealService.getSetmealReport();


        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    //导出预约表；
    @RequestMapping("/exportReservationForm")
    public Result exportReservationForm(HttpServletResponse response) {
        try {
            List<Map<String, Object>> list = orderSettingService.findAll();
//        创建一个excel文件
            XSSFWorkbook excel = new XSSFWorkbook();
            //创建一个表
            XSSFSheet sheet = excel.createSheet("预约数据");
            //在工作表创建行
            XSSFRow title = sheet.createRow(0);
            //在行中创建单元格；
            title.createCell(0).setCellValue("日期");
            title.createCell(1).setCellValue("可预约人数");
            title.createCell(2).setCellValue("已预约人数");
            for (int i = 0; i < list.size(); i++) {
                XSSFRow data = sheet.createRow(i + 1);
                Map<String, Object> map = list.get(i);
                String orderDate = new SimpleDateFormat("yyyy-MM-dd").format(map.get("orderDate"));
                data.createCell(0).setCellValue(orderDate);
                data.createCell(1).setCellValue( map.get("number").toString());
                data.createCell(2).setCellValue(map.get("reservations").toString());
            }
            //使用输出流进行表格下载
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=reportReservationForm.xlsx" );
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
            excel.close();

        return null;
        } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"下载失败" );
        }
    }

    @RequestMapping("/exportHealthQuestionnaire")
    public String exportHealthQuestionnaire(HttpServletResponse response){
        try {
            //首先去找到这些数据；
            List<Map<String,Object>> list = assessmentService.findAllAndName();
            //然后创建excel表进行装数据下载；
            //创建一个excel文件
            XSSFWorkbook excel = new XSSFWorkbook();
            //创建一个表
            XSSFSheet sheet = excel.createSheet("健康评估问卷");
            //在工作表创建行
            XSSFRow title = sheet.createRow(0);
            //在行中创建单元格；
            title.createCell(0).setCellValue("档案号");
            title.createCell(1).setCellValue("姓名");
            title.createCell(2).setCellValue("测评日期");
            title.createCell(3).setCellValue("阳虚体质");
            title.createCell(4).setCellValue("心理评分");
            for (int i = 0; i < list.size(); i++) {
                XSSFRow data = sheet.createRow(i + 1);
                Map<String, Object> map = list.get(i);
                data.createCell(0).setCellValue(map.get("fileNumber").toString());
                data.createCell(1).setCellValue((String) map.get("name"));
                if (map.get("assessDate")!=null){
                    data.createCell(2).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(map.get("assessDate")));
                }else {
                    data.createCell(2).setCellValue("未测评");
                }
                if (map.get("sun_Sum")!=null){
                    int sun_sum = (int) map.get("sun_Sum");
                    if (sun_sum*1.0 < 28 *0.75){
                        data.createCell(3).setCellValue("是");
                    }else {
                        data.createCell(3).setCellValue("否");
                    }
                }else {
                    data.createCell(3).setCellValue("未测评");
                }
                if (map.get("mentality_Sum")!=null){
                    int mentality_sum = (int) map.get("mentality_Sum");
                    if (mentality_sum*1.0 < 40 * 0.75){
                        data.createCell(4).setCellValue("是");
                    }else {
                        data.createCell(4).setCellValue("否");
                    }
                }else {
                    data.createCell(4).setCellValue("未测评");
                }
            }
            //使用输出流进行表格下载
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=reportHealthQuestionnaire.xlsx" );
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
            excel.close();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return  "下载失败";
        }
    }

    /**
     * 权限管理-文件下载
     * @param response
     * @return
     */
    @RequestMapping("/fileDownload.do")
    public Result fileDownload(HttpServletResponse response) {
        try {
            List<Map<String, Object>> list = userService.findUserManagement();
              // 创建一个excel文件
            XSSFWorkbook excel = new XSSFWorkbook();
            //创建一个表
            XSSFSheet sheet = excel.createSheet("用户管理");
            //在工作表创建行
            XSSFRow title = sheet.createRow(0);
            //在行中创建单元格；
            title.createCell(0).setCellValue("用户名");
            title.createCell(1).setCellValue("性别");
            title.createCell(2).setCellValue("出生日期");
            title.createCell(3).setCellValue("电话号码");
            title.createCell(4).setCellValue("状态");
            title.createCell(5).setCellValue("备注");
            for (int i = 0; i < list.size(); i++) {
                XSSFRow data = sheet.createRow(i + 1);
                Map<String, Object> map = list.get(i);

                if (map.get("username") != null){
                    data.createCell(0).setCellValue(map.get("username").toString());
                }else {
                    data.createCell(0).setCellValue("无");
                }

                if (map.get("gender") != null ){
                    data.createCell(1).setCellValue( map.get("gender").toString());
                }else {
                    data.createCell(1).setCellValue("无");
                }

                if (map.get("birthday") != null ){
                    String birthday = new SimpleDateFormat("yyyy-MM-dd").format(map.get("birthday"));
                    data.createCell(2).setCellValue(birthday);
                }else {
                    data.createCell(2).setCellValue("无");
                }

                if (map.get("telephone") != null ){
                    data.createCell(3).setCellValue(map.get("telephone").toString());
                }else {
                    data.createCell(3).setCellValue("无");
                }

                if (map.get("station") != null ){
                    data.createCell(4).setCellValue(map.get("station").toString());
                }else {
                    data.createCell(4).setCellValue("无");
                }

                if (map.get("remark") != null ){
                    data.createCell(5).setCellValue(map.get("remark").toString());
                }else {
                    data.createCell(5).setCellValue("无");
                }


            }
            //使用输出流进行表格下载
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=userManagementReport.xlsx" );
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
            excel.close();

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"下载失败" );
        }
    }

}
